package com.cskaoyan.controller;

import com.cskaoyan.bean.bo.AdminOrderListBo;
import com.cskaoyan.bean.bo.AdminOrderRefundBo;
import com.cskaoyan.bean.bo.AdminOrderReplyBO;
import com.cskaoyan.bean.bo.AdminOrderShipBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.AdminOrderChannelVo;
import com.cskaoyan.bean.vo.AdminOrderShipVo;
import com.cskaoyan.bean.vo.admin_order_list.AdminOrderListVo;
import com.cskaoyan.service.AdminOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


/**
 * @author Trellis
 * @since 2022/09/06 18:43
 */
@RestController
@RequestMapping("admin/order")
public class AdminOrderController {

    @Autowired
    AdminOrderService adminOrderService;



    @PostMapping("delete")
    public BaseRespVo deleteOrder(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        adminOrderService.deleteOrder(orderId);
        return BaseRespVo.ok();
    }

    /**
     * 1、获取请求参数，get方法
     * 2、执行业务逻辑，根据获取的参数，到数据库中查找
     * 3、返回响应信息 json类型
     *
     * @param listBo
     * @return com.cskaoyan.bean.vo.admin_order_list.AdminOrderListVo
     * @author Trellis
     * @since 2022/09/06 19:03
     */

    //http://182.92.235.201:8083/admin/order/list
    // ?page=1&limit=20&orderStatusArray=201&sort=add_time&order=desc&orderId=&start=&end=&userId=1&orderSn=20220913048545
    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String start,String end,Short orderStatusArray,Integer orderId,Integer userId,String orderSn) throws ParseException {
        // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // Date startTime = dateFormat.parse(start);
        // Date endTime = dateFormat.parse(end);

        AdminOrderListBo listBo = new AdminOrderListBo(page,limit,sort,order,start,end,orderStatusArray,orderId,userId,orderSn);
        BaseRespVo listByBo = adminOrderService.getListByBo(listBo);
        return listByBo;
    }


    /**
     * 1、获取请求参数，参数为order的id
     * 2、执行业务逻辑，根据order的id 在数据库中查找相关信息
     * 3、返回响应信息，json类型
     *
     * @param id
     * @return com.cskaoyan.bean.vo.admin_order_detail.AdminOrderDetailVo
     * @author Trellis
     * @since 2022/09/07 9:09
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        BaseRespVo detailById = adminOrderService.getDetailById(id);
        return detailById;
    }

    /**
     * 1、获取请求参数
     * 2、执行业务逻辑 根据order的id 更改订单的状态
     * 3、返回响应信息  json类型
     *
     * @param shipBo
     * @return com.cskaoyan.bean.vo.AdminOrderShipVo
     * @author Trellis
     * @since 2022/09/07 13:58
     */
    @PostMapping("ship")
    public AdminOrderShipVo ship(@RequestBody AdminOrderShipBo shipBo) {
        int orderId = shipBo.getOrderId();
        Short status = 301;
        AdminOrderShipVo adminOrderShipVo = adminOrderService.updateStatusByOrderId(shipBo,orderId, status);
        return adminOrderShipVo;
    }

    /**1、获取请求参数
     * 2、执行业务逻辑 根据order的id 更改订单状态
     * 3、返回响应信息
     * 4、此方法复用了shipHandler的响应信息
     * @param refundBo
     * @return com.cskaoyan.bean.vo.AdminOrderShipVo
     * @author Trellis
     * @since 2022/09/07 14:57
     */
    @PostMapping("refund")
    public BaseRespVo refund(@RequestBody AdminOrderRefundBo refundBo) {
        int orderId = refundBo.getOrderId();
        Short status = 203;
        BaseRespVo baseRespVo = adminOrderService.updateStatusByBo(refundBo, status, orderId);
        return baseRespVo;
    }


    @RequestMapping("channel")
    public AdminOrderChannelVo channel() {
        AdminOrderChannelVo channel = adminOrderService.getChannel();
        return channel;
    }

    /*
     * /admin/order/reply
     * POST
     * */
    @RequestMapping("reply")
    public BaseRespVo reply(@RequestBody AdminOrderReplyBO adminOrderReplyBO) {
        BaseRespVo ok = BaseRespVo.ok();
        int code = adminOrderService.reply(adminOrderReplyBO);
        if (code == 622) {
            ok.setErrno(622);
            ok.setErrmsg("评论已回复");
        }
        return ok;

    }
}
